package butines.kraz;

import java.awt.Color;
import java.awt.Graphics2D;

import butines.core.Game;
import butines.core.GameContainer;
import butines.core.GameContext;
import butines.core.GameSystem;
import butines.core.Input;
import butines.core.SpriteSheet;
import butines.kraz.entity.Unit;
import butines.kraz.entity.weapon.WeaponMelee;
import butines.kraz.entity.weapon.WeaponRange;

public class KrazMain implements Game {

	private GameContext ctx;
	private Unit unit;
	private Unit enemy;

	private Kraz kraz;
	
	private SpriteSheet siFootman;
	private SpriteSheet siHumanCorpse;
	
	private Unit createEnemy(float x, float y) {
	
		enemy = new Unit();
		enemy.setRadius(16);
		enemy.setSprite(siFootman);
		enemy.setSheetCorpse(siHumanCorpse);
		enemy.getPosition().set(x, y);
		enemy.stay();
		kraz.addUnit(enemy);
		
		return enemy;
	}
	
	public void init(GameContext ctx) {
		this.ctx = ctx;
		
		kraz = new Kraz(800, 640);
		
		SpriteSheet siCursorArrow = new SpriteSheet("datas/ui/cursor_arrow.png", 128, 128);
		siCursorArrow.addAnimation(true, .5f, 0, 1, 2, 3, 4);
		
		SpriteSheet siCursorDrag = new SpriteSheet("datas/ui/cursor_drag.png", 128, 128);
		siCursorDrag.addAnimation(true, .5f, 0, 1, 2, 3);
		
		SpriteSheet siHammer = new SpriteSheet("datas/projectile/gryphon_hammer.png", 32, 32, 8);
		siHammer.addAnimation(true, 0.7f, 0, 1, 2);
		
		SpriteSheet siArrow = new SpriteSheet("datas/projectile/arrow.png", 40, 40, 8);
		siArrow.addAnimation(false, 0, 0);
		
		SpriteSheet siArcher = new SpriteSheet("datas/unit/elven_archer.png", 72, 72, 8);
		siArcher.addAnimation("stay", false, 0, 0);
		siArcher.addAnimation("walk", true, .7f, 1, 2, 0, 3, 4, 0);
		siArcher.addAnimation("attack", false, 1f, 4, 5, 6, 0);
		siArcher.addAnimation("die", false, .3f, 0, 7, 8, 9);
		siArcher.addAnimation("dead", false, 3f, 9);
		
		siFootman = new SpriteSheet("datas/unit/footman.png", 72, 72, 8);
		siFootman.addAnimation("stay", false, 0, 0);
		siFootman.addAnimation("walk", true, .7f, 1, 2, 0, 3, 4, 0);
		siFootman.addAnimation("attack", false, .5f, 5, 6, 7, 8, 0);
		siFootman.addAnimation("die", false, .3f, 0, 9, 10, 11);
		siFootman.addAnimation("dead", false, 3f, 11);
		
		siHumanCorpse = new SpriteSheet("datas/unit/corpses.png", 72, 72, 8);
		siHumanCorpse.addAnimation(false, 20f, 0, 2, 3, 4, 5);
		
		WeaponRange bow = new WeaponRange();
		bow.setCooldown(1f);
		bow.setPerform(.5f);
		bow.setDamage(5);
		bow.setKraz(kraz);
		bow.setRange(50);
		bow.setSpeed(10f);
//		bow.setSheet(siHammer);
		bow.setSheet(siArrow);
		
		
		WeaponMelee sword = new WeaponMelee();
		sword.setCooldown(1f);
		sword.setPerform(.5f);
		sword.setDamage(15);
		sword.setKraz(kraz);
		sword.setRange(15);
		
		unit = new Unit();
		unit.setRadius(16);
		unit.setSprite(siArcher);
		unit.setSheetCorpse(siHumanCorpse);
//		unit.getSprite().setSheet(siFootman);
//		unit.getSprite().addAnimation("stay", 0, 0);
//		unit.getSprite().addAnimation("walk", .5f, 1, 2, 0, 3, 4, 0);
//		unit.getSprite().addAnimation("attack", .3f, 5, 6, 7, 8);
		unit.getPosition().set(120, 200);
		unit.setMaxForce(.1f);
		unit.setMaxSpeed(1f);
		unit.setWeapon(bow);
		unit.stay();
		kraz.addUnit(unit);
		
//		createEnemy(90, 130).setWeapon(sword); createEnemy(90, 150).setWeapon(sword); createEnemy(90, 170).setWeapon(sword);
//		createEnemy(110, 130).setWeapon(sword); createEnemy(110, 150).setWeapon(sword); createEnemy(110, 170).setWeapon(sword);
		createEnemy(130, 130).setWeapon(sword); createEnemy(130, 150).setWeapon(sword); createEnemy(130, 170).setWeapon(sword);
		
		enemy = createEnemy(250, 100);
		enemy.setWeapon(sword);
//		enemy.attack(unit);
		enemy.hold();
		
	}

	public void destroy(GameContext ctx) {
		
	}

	public void update(GameContext ctx, float tpf) {
		kraz.update(tpf);
	}

	public void render(GameContext ctx, Graphics2D g) {
		g.setColor(Color.black);
		g.fillRect(0, 0, ctx.getScreenWidth(), ctx.getScreenHeight());
				
		kraz.draw(g);
		
	}

	public static void main(String[] args) {
		GameContainer gameContainer = GameSystem.getContainer();
		gameContainer.setDisplayMode(400, 300, 32);
		gameContainer.setFullScreen(false);
		gameContainer.setTargetFps(60);
		gameContainer.start(new KrazMain());
	}

	public void onKey(int keyCode, boolean pressed, float tpf) {
		if (Input.KEY_ESC == keyCode) {
			ctx.exit();
		}
	}

	public void onButton(int x, int y, int button, boolean pressed, float tpf) {
//		if (button == Input.BUTTON_1 && pressed) {
//			unit.attack(enemy);
//		}
//		if (button == Input.BUTTON_3 && pressed) {
//			unit.move(new Vector2f(x, y));
//		}
		kraz.onButton(x, y, button, pressed);
	}

	public void onMove(int x, int y, int dx, int dy, float tpf) {
		kraz.onMove(x, y);
	}
	
}
